
<cfif IsDefined( "Session.SmugUser.SessionID" )>
	<cfset Request.SessionID = Session.SmugUser.SessionID />
</cfif>

<cfswitch expression="#LCase( Trim( Request.do ) )#">

	<cfcase value="auth.anonymously">
		<cfset sfAuth = createObject( "component", "smug.CFSmugAuth" ).init( config = Application.CFSmugConfigObj ) />

		<cfset sessionID = sfAuth.anonymously() />

		<cflock scope="Session" type="exclusive" timeout="10">
			<cfset Session.SmugUser = StructNew() />
			<cfset Session.SmugUser.SessionID = sessionID />
			<cfset Session.SmugUser.LoggedIn = false />
		</cflock>

	</cfcase>

	<cfcase value="auth.password">
		<cfset sfAuth = createObject( "component", "smug.CFSmugAuth" ).init( config = Application.CFSmugConfigObj ) />

		<cfset userStruct = sfAuth.withPassword( ArgumentCollection = Request ) />

		<cflock scope="Session" type="exclusive" timeout="10">
			<cfset Session.SmugUser = StructNew() />
			<cfset Session.SmugUser = userStruct />
			<cfset Session.SmugUser.LoggedIn = true />
		</cflock>

	</cfcase>

	<cfcase value="auth.hash">
		<cfset sfAuth = createObject( "component", "smug.CFSmugAuth" ).init( config = Application.CFSmugConfigObj ) />

		<cfset userStructHash = sfAuth.withHash( ArgumentCollection = Session.SmugUser ) />

		<cflock scope="Session" type="exclusive" timeout="10">
			<cfset StructAppend( Session.SmugUser, userStructHash ) />
			<cfset Session.SmugUser.LoggedIn = true />
		</cflock>

	</cfcase>

	<cfcase value="auth.logout">
		<cfset sfAuth = createObject( "component", "smug.CFSmugAuth" ).init( config = Application.CFSmugConfigObj ) />

		<cfset sfAuth.logout( ArgumentCollection = Session.SmugUser ) />

		<cflock scope="Session" type="exclusive" timeout="10">
			<cfset Session.SmugUser = StructNew() />
		</cflock>

	</cfcase>

	<cfcase value="albums.copy">
		<cfset sfAlbums = createObject( "component", "smug.CFSmugAlbums" ).init( config = Application.CFSmugConfigObj ) />
		<cfset Request.CFSmugImagesCFC = createObject( "component", "smug.CFSmugImages" ).init( config = Application.CFSmugConfigObj ) />

		<cfset result = sfAlbums.copy( ArgumentCollection = Request ) />

		<cflock scope="Session" type="exclusive" timeout="10">
			<cfset Session.SmugUser = StructNew() />
		</cflock>

	</cfcase>

	<cfcase value="images.upload">
		<cfset sfImages = createObject( "component", "smug.CFSmugImages" ).init( config = Application.CFSmugConfigObj ) />

		<cffile action="upload"
				destination="#GetTempDirectory()#"
				nameconflict="overwrite"
				filefield="Form.File"
				result="Variables.fileInfo" />

		<cfset Variables.serverFile = Variables.fileInfo.SERVERDIRECTORY &
									  Application.config.FileSeparator &
									  Variables.fileInfo.SERVERFILE />

		<cffile action="readbinary"
				file="#Variables.serverFile#"
				variable="Request.Data" />

		<cfset Request.FileName = Variables.fileInfo.ClientFile />

		<cfset Request.NewImage = sfImages.upload( ArgumentCollection = Request ) />
	</cfcase>


	<cfdefaultcase>
		<cfset CFSmugObject = createObject( "component", "smug.CFSmug#ListFirst( Request.do, "." )#" ).init( config = Application.CFSmugConfigObj ) />

		<cfset result = evaluate( "CFSmugObject.#ListLast( Request.do, "." )#( ArgumentCollection = Request )" ) />

	</cfdefaultcase>

</cfswitch>

<!--- if not redirected... --->
<cfinclude template="view.cfm" />